import http from './http';

export const userApi = {
  /**
   * 用户注册
   */
  async register(data) {
    const response = await http.post('/user/register', data);
    if (response.data.code === 0) return response.data.data;
    throw new Error(response.data.message || '注册失败');
  },

  /**
   * 用户登录
   */
  async login(data) {
    const response = await http.post('/user/login', data);
    if (response.data.code === 0) return response.data.data;
    throw new Error(response.data.message || '登录失败');
  },

  /**
   * 重置密码
   */
  async resetPassword(data) {
    const response = await http.post('/user/reset/password', data);
    if (response.data.code === 0) return response.data.data;
    throw new Error(response.data.message || '重置密码失败');
  },

  /**
   * 修改用户信息
   */
  async update(data) {
    const response = await http.post('/user/update', data);
    if (response.data.code === 0) return response.data.data;
    throw new Error(response.data.message || '修改用户信息失败');
  },

  /** 获取当前登录用户信息 */
  async getCurrentUser() {
    const response = await http.get('/user/current');
    if (response.data.code === 0) return response.data.data;
    throw new Error(response.data.message || '获取用户信息失败');
  },

  /** 用户登出 */
  async logout() {
    const response = await http.post('/user/logout');
    if (response.data.code === 0) return response.data.data;
    throw new Error(response.data.message || '登出失败');
  }
};
